﻿<%@ WebHandler Language="C#" Class="getMovies" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class getMovies : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //指定回送数据的类型
        context.Response.ContentType = "application/json";
        string tid = context.Request["tid"];
        string action = context.Request["action"];
        string searchType = context.Request["type"];
        string searchWord = context.Request["kerword"];
        
        //准备回送的字符串
        string outstr = "";

        //执行写入操作
        outstr = doSelect(tid, action, searchType, searchWord);

        //回送结果
        context.Response.Write(outstr);
    }

    public string doSelect(string tid, string action, string searchType, string searchWord)
    {
        
        string sql = "select * from movielist";
        if (action == "search")
        {
            sql = "select movielist.id, mname, maker, url, img, playtimes, isrecommend, onlinetime, breif, types.tname from movielist, types where types.id = movielist.tid ";

            if (searchType.ToString() != "0")
            {
                sql += " and types.id=" + searchType;
            }
            if (!searchWord.Equals(""))
            {
                sql += " and mname like '%" + searchWord + "%'";
            }
            
            
            sql += " order by onlinetime desc";

        }
        else
        {
            if (tid == null)
            {
                //执行查询操作，并获得返回的行数
                sql = "select movielist.id, mname, maker, url, img, playtimes, isrecommend, onlinetime, breif, types.tname from movielist, types where types.id = movielist.tid";
                
            }
            else
            {
                //执行查询操作，并获得返回的行数
                sql = "select movielist.id, mname, maker, url, img, playtimes, isrecommend, onlinetime, breif, types.tname from movielist, types where types.id = movielist.tid and movielist.id = '" + tid + "'";
            }
        }
        DataSet ds = DBHelper.sqlMethod(sql);
        int i = ds.Tables[0].Rows.Count;


        StringBuilder Builder = new StringBuilder();
        Builder.Append("{");
        Builder.Append("\"r\":" + i + "");
        if (i > 0)
        {
            List<Movies> list = new List<Movies>();

            for (int j = 0; j < i; j++)
            {
                Movies temp = new Movies(int.Parse(ds.Tables[0].Rows[j][0].ToString()), ds.Tables[0].Rows[j][1].ToString(), ds.Tables[0].Rows[j][2].ToString(), ds.Tables[0].Rows[j][3].ToString(), ds.Tables[0].Rows[j][4].ToString(), int.Parse(ds.Tables[0].Rows[j][5].ToString()), ds.Tables[0].Rows[j][6].ToString(), ds.Tables[0].Rows[j][7].ToString(), ds.Tables[0].Rows[j][8].ToString(), ds.Tables[0].Rows[j][9].ToString());
                list.Add(temp);
            }

            string json = new JavaScriptSerializer().Serialize(list);//这个很关键，否则error 

            Builder.Append(",\"data\":");
            Builder.Append(json);
        }

        Builder.Append("}");
        return Builder.ToString();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}